package com.tarena.luban.demo.rocket.transaction;

import lombok.extern.slf4j.Slf4j;
import org.apache.rocketmq.spring.annotation.RocketMQTransactionListener;
import org.apache.rocketmq.spring.core.RocketMQLocalTransactionListener;
import org.apache.rocketmq.spring.core.RocketMQLocalTransactionState;
import org.springframework.messaging.Message;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;

@Component
@Slf4j
@RocketMQTransactionListener
public class TransactionMsgListener implements RocketMQLocalTransactionListener {
    //执行本地事务的方法

    /**
     * @param msg 消息对象
     * @param arg 业务数据对象
     * @return 处理本地事务返回的状态值 COMMIT ROLLBACK UNKNOWN
     */
    @Override
    public RocketMQLocalTransactionState executeLocalTransaction(Message msg, Object arg) {
        System.out.println("半消息传递的业务数据:"+arg);
        //返回的状态非常明确 commit
        return RocketMQLocalTransactionState.UNKNOWN;
    }

    @Override
    public RocketMQLocalTransactionState checkLocalTransaction(Message msg) {
        log.info("回调方法调用了，当前系统时间：{}", LocalDateTime.now());
        return RocketMQLocalTransactionState.ROLLBACK;
    }
}
